﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;

using Bierdopje.API;

namespace Bierdopje.API.Personal
{
    public class ApiPersonal
    {
        private const string cCalendarPre = @"http://external.bierdopje.com/calendar/";
        private const string cCalendarPost = @"/xml/";

        private string m_PersonalKey;

        public string PersonalKey
        {
            get { return m_PersonalKey; }
            set { m_PersonalKey = value; }
        }

        private ApiLibrary m_ApiLibrary;

        /// <summary>
        /// Gets the users personal calendar
        /// </summary>
        /// <returns>Personal calendar</returns>
        public Calendar getPersonalCalendar()
        {
            Calendar _NewCalendar = new Calendar();

            string CalendarResponse = HTTPHelper.GetHTTPResponse(cCalendarPre + PersonalKey + cCalendarPost);

            if (!CalendarResponse.Contains("<"))
                throw new EApiPersonalKeyIncorrect();

            CalendarResponse = XMLHelper.XMLCleanup(CalendarResponse);

            XmlDocument _XDoc = new XmlDocument();
            _XDoc.LoadXml(CalendarResponse);

            int DateAdder = 0;
            foreach (XmlNode _DateNode in _XDoc.SelectNodes(@"/bierdopje/day"))
            {
                string dateString = _DateNode.Attributes["date"].Value;

                // Fix for bug: http://www.bierdopje.com/forum/bugs/topic/3910--XML--Calendar-Datum-issue---req/last
                DateTime _date;
                if (dateString.Equals(""))
                {
                    _date = new DateTime(DateTime.Now.Year,DateTime.Now.Month,1);
                    DateAdder = 1;
                }
                else
                {
                    _date = DateTime.ParseExact(dateString, "dd-MM-yyyy", CultureInfo.InvariantCulture).AddDays(DateAdder);
                }

                CalendarDay _Day = new CalendarDay(_date);

                foreach (XmlNode _EpisodeNode in _DateNode.SelectNodes(@"episode"))
                {
                    CalendarEpisode _Episode = new CalendarEpisode(m_ApiLibrary);
                    _Episode.EpisodeName = XMLHelper.ExtractStringFromNode(_EpisodeNode, "epname");
                    _Episode.EpisodeUrl = XMLHelper.ExtractStringFromNode(_EpisodeNode, "epurl");
                    _Episode.ShowName = XMLHelper.ExtractStringFromNode(_EpisodeNode, "showname");
                    _Episode.EpisodeSEString = XMLHelper.ExtractStringFromNode(_EpisodeNode, "urlid");
                    _Episode.ShowUrl = XMLHelper.ExtractStringFromNode(_EpisodeNode, "showurl");
                    _Episode.EpisodeID = XMLHelper.ExtractIntFromNode(_EpisodeNode, "episodeid");
                    _Episode.ShowID = XMLHelper.ExtractIntFromNode(_EpisodeNode, "showid");
                    _Day.Episodes.Add(_Episode);
                }
                _NewCalendar.Days.Add(_Day);
            }

            return _NewCalendar;
        }
    
        public ApiPersonal(string personalKey)
        {
            m_PersonalKey = personalKey;
        }

        public ApiPersonal(string personalKey, ApiLibrary api)
        {
            m_PersonalKey = personalKey;
            m_ApiLibrary = api;
        }

    }
}
